package com.project.mvprxsample.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.project.mvprxsample.ui.fragment.BaseFragment;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by lvyang on 2017/5/15.
 */
public abstract class FragmentBaseActivity extends BaseActivity implements BaseFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //避免重复添加Fragment
        if (null == getSupportFragmentManager().getFragments()) {
            BaseFragment firstFragment = getFirstFragment();
            if (null != firstFragment && (!firstFragment.isAdded())) {
                addFragment(firstFragment);
            }
        }
    }

    /**
     * 初始化标题栏
     */
    @Override
    protected void initTitle() {

    }

    /**
     * 初始化控件
     */
    @Override
    protected void initView() {

    }

    /**
     * 初始化presenters
     */
    @Override
    protected void initPresenters() {

    }

    /**
     * 点击menu菜单事件
     *
     * @return
     */
    @Override
    protected void doActionMenu() {

    }

    @Override
    protected boolean hideActionBar() {
        return false;
    }

    @Override
    protected Drawable getActionMenuIcon() {
        return null;
    }

    @Override
    protected String getActionMenuText() {
        return null;
    }

    /**
     * 获取第一个fragment
     *
     * @return
     */
    protected abstract BaseFragment getFirstFragment();

    /**
     * 添加fragment
     *
     * @param fragment
     */
    public void addFragment(BaseFragment fragment) {
        if (fragment != null && (!fragment.isAdded())) {
            getSupportFragmentManager().beginTransaction()
                    .replace(getFragmentContentId(), fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    //布局中Fragment的ID
    protected abstract int getFragmentContentId();


    /**
     * 获取当前正在显示的Fragment
     *
     * @return
     */
    public BaseFragment getCurrentFragment() {

        List<Fragment> list = getFragmentList();

        for (int i = 0; i < list.size(); i++) {
            if (null != list.get(i) && list.get(i).isVisible()) {
                return (BaseFragment) list.get(i);
            }
        }

        if (list.size() >= 1) {
            return (BaseFragment) list.get(list.size() - 1);
        } else {
            return null;
        }
    }

    /**
     * 获取所有加载的Fragment
     *
     * @return
     */
    public List<Fragment> getFragmentList() {
        return getSupportFragmentManager().getFragments();
    }


    /**
     * replaceFragment
     *
     * @param fragment
     */
    public void replaceFragment(BaseFragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(getFragmentContentId(), fragment, fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    /**
     * removeFragment
     *
     * @param fragment
     */
    public void removeFragment(BaseFragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
        }
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            removeFragment(getCurrentFragment());
            super.onBackPressed();
        } else {
            finish();
        }
    }

    @Override
    public void onFragmentInteraction(String tag, JSONObject json) {

    }
}
